package com.ajie.controller;

import com.ajie.config.annotation.RestControllerMapping;
import com.ajie.model.PageParams;
import com.ajie.model.Result;
import com.ajie.entity.Motion;
import com.ajie.service.MotionService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;

/**
 * 运动咨询;(motion)表控制层
 * @author: ajie
 * @date : 2024-7-27
 */
@Tag(name = "运动咨询")
@RestControllerMapping("/motion")
@ApiSupport(author = "ajie:20999@163.com", order = 9)
public class MotionController {

    @Resource
    private MotionService motionService;

    @PostMapping("page")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "分页查询")
    public Result<Page<Motion>> pageQuery(@RequestBody PageParams<Motion> pageParams) {
        // 1.查询参数
        Motion params = pageParams.getParams();
        LambdaQueryWrapper<Motion> lqw = new LambdaQueryWrapper<>();
        // 2.查询条件
        lqw.like(params.getName() != null, Motion::getName, params.getName());
        lqw.like(params.getBeneficialPosition() != null, Motion::getBeneficialPosition, params.getBeneficialPosition());

        // 3.分页查询
        return Result.success(motionService.page(pageParams.getPage(), lqw));
    }

    @PostMapping("insert")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "新增数据")
    public Result<Boolean> insert(@RequestBody Motion motion) {
        if (motionService.save(motion)) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("update")
    @ApiOperationSupport(order = 3)
    @Operation(summary = "更新数据")
    public Result<Boolean> update(@RequestBody Motion motion) {
        if (motionService.updateById(motion)) {
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("/delete/{id}")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "通过主键删除数据", parameters = {
            @Parameter(name = "id", description = "主键", required = true, in = ParameterIn.PATH)
    })
    public Result<Boolean> deleteById(@PathVariable String id){
        if (motionService.removeById(id)) {
            return Result.success();
        }
        return Result.error();
    }
}